class_name UnitDragon
extends UnitFlyMagic


func shoot_magic():
	if not attack_target: return
	var target_unit = attack_target as UnitBase
	var dist_per_light: float = 64
	
	for index in range(5):
		shoot_lightning_magic(Vector2.RIGHT * (-dist_per_light * 2 + index * dist_per_light))


func shoot_lightning_magic(offset: Vector2):
	if not attack_target: return
	
	var target_unit = attack_target as UnitBase
	
	var result = GlobalUtils.raycast_to(target_unit.ray_cast_point.global_position + offset, target_unit.ray_cast_point.global_position + offset + Vector2.DOWN * 1000, 1)
	if result:
		await get_tree().create_timer(0.2).timeout
		
		var magic = (ResourceManager.get_resource("lightning_magic") as PackedScene).instantiate() as LightningMagic
		
		var config = unit_data.config 
		magic.attack = config.main_attack
		magic.critical_rate = config.main_critical_attack_rate
		magic.critical_bonus = config.main_critical_attack_bonus
		magic.knockback = config.main_attack_knockback
		magic.sender = self
		magic.need_tip = true
		
		level.particles_parent.add_child(magic)
		magic.global_position = result["position"]
		magic.set_is_enemy(true)


func play_magic_animation():
	animation_player.play("magic")
